<template>
  <div class="list-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <nav class="nav-container">
        <div class="nav-left">
          <button class="back-btn" @click="goBack">
            <span class="back-icon">←</span>
            返回
          </button>
          <h1 class="page-title">通知列表</h1>
        </div>
      </nav>
    </header>

    <!-- 主要内容区域 -->
    <main class="main-content">
      <div class="list-wrapper" v-if="!loading">
        <div v-if="notificationsList.length === 0" class="empty-state">
          <div class="empty-icon">📢</div>
          <p class="empty-text">暂无通知信息</p>
        </div>
        
        <div v-else class="items-grid">
          <div 
            v-for="notification in notificationsList" 
            :key="notification.id" 
            class="list-item"
            @click="handleItemClick(notification)"
          >
            <div class="item-header">
              <h3 class="item-title">{{ notification.title }}</h3>
              <span class="item-date">{{ formatDate(notification.publishTime) }}</span>
            </div>
            
            <div class="item-content">
              <p class="item-summary">{{ notification.summary || notification.content }}</p>
            </div>
            
            <div class="item-footer">
              <span class="item-type">通知</span>
              <span class="item-priority" :class="getPriorityClass(notification.viewCount)">
                {{ getPriorityText(notification.viewCount) }}浏览
              </span>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-state">
        <div class="loading-spinner"></div>
        <p>加载中...</p>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getAllNotificationsService } from '@/api/home.js'

const router = useRouter()
const notificationsList = ref([])
const loading = ref(true)

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}

// 获取优先级样式类
const getPriorityClass = (priority) => {
  switch (priority) {
    case 'high':
    case '高':
    case '重要':
      return 'priority-high'
    case 'medium':
    case '中':
    case '一般':
      return 'priority-medium'
    case 'low':
    case '低':
      return 'priority-low'
    default:
      return 'priority-default'
  }
}

// 获取优先级文本
const getPriorityText = (priority) => {
  switch (priority) {
    case 'high':
      return '重要'
    case 'medium':
      return '一般'
    case 'low':
      return '普通'
    default:
      return priority || '一般'
  }
}

// 点击项目
const handleItemClick = (notification) => {
  router.push(`/notification-detail/${notification.id}`)
}

// 加载通知列表
const loadNotificationsList = async () => {
  try {
    loading.value = true
    const result = await getAllNotificationsService()
    if (result.code === 200 && result.data) {
      // 后端返回的是分页数据，需要取list字段
      notificationsList.value = result.data.list || []
    }
  } catch (error) {
    console.error('加载通知列表失败:', error)
    notificationsList.value = []
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  loadNotificationsList()
})
</script>

<style scoped lang="scss">
//@import '@/assets/css/notification-list.css';
.list-container {
    min-height: 100vh;
    background: #f5f5f5;
  }
  
  /* 顶部导航栏 */
  .header {
    background: linear-gradient(135deg, #6278db 0%, #635da6 100%);
    padding: 0;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    position: sticky;
    top: 0;
    z-index: 100;
  }
  
  .nav-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 15px 20px;
  }
  
  .nav-left {
    display: flex;
    align-items: center;
    gap: 20px;
  }
  
  .back-btn {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.3);
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 6px;
  }
  
  .back-btn:hover {
    background: rgba(255, 255, 255, 0.3);
  }
  
  .back-icon {
    font-size: 16px;
  }
  
  .page-title {
    color: white;
    font-size: 20px;
    font-weight: 600;
    margin: 0;
  }
  
  /* 主要内容区域 */
  .main-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 30px 20px;
  }
  
  .list-wrapper {
    background: white;
    border-radius: 15px;
    padding: 30px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  }
  
  /* 空状态 */
  .empty-state {
    text-align: center;
    padding: 60px 20px;
  }
  
  .empty-icon {
    font-size: 64px;
    margin-bottom: 20px;
    opacity: 0.6;
  }
  
  .empty-text {
    color: #999;
    font-size: 16px;
    margin: 0;
  }
  
  /* 项目网格 */
  .items-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 20px;
  }
  
  .list-item {
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 12px;
    padding: 20px;
    cursor: pointer;
    transition: all 0.3s ease;
  }
  
  .list-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
    border-color: #6278db;
  }
  
  .item-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 15px;
  }
  
  .item-title {
    color: #333;
    font-size: 18px;
    font-weight: 600;
    margin: 0;
    flex: 1;
    margin-right: 15px;
    line-height: 1.4;
  }
  
  .item-date {
    color: #666;
    font-size: 12px;
    white-space: nowrap;
    background: #e9ecef;
    padding: 4px 8px;
    border-radius: 4px;
  }
  
  .item-content {
    margin-bottom: 15px;
  }
  
  .item-summary {
    color: #666;
    font-size: 14px;
    line-height: 1.6;
    margin: 0;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  
  .item-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .item-type {
    background: #17a2b8;
    color: white;
    font-size: 12px;
    padding: 4px 12px;
    border-radius: 12px;
    font-weight: 500;
  }
  
  .item-priority {
    font-size: 12px;
    padding: 4px 8px;
    border-radius: 4px;
    font-weight: 500;
  }
  
  .priority-high {
    background: #f8d7da;
    color: #721c24;
  }
  
  .priority-medium {
    background: #fff3cd;
    color: #856404;
  }
  
  .priority-low {
    background: #d4edda;
    color: #155724;
  }
  
  .priority-default {
    background: #e2e3e5;
    color: #495057;
  }
  
  /* 加载状态 */
  .loading-state {
    text-align: center;
    padding: 60px 20px;
  }
  
  .loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid #6278db;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 20px;
  }
  
  @keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
  }
  
  /* 移动端适配 */
  @media (max-width: 768px) {
    .nav-container {
      padding: 12px 15px;
    }
    
    .page-title {
      font-size: 18px;
    }
    
    .main-content {
      padding: 20px 15px;
    }
    
    .list-wrapper {
      padding: 20px;
    }
    
    .items-grid {
      grid-template-columns: 1fr;
      gap: 15px;
    }
    
    .list-item {
      padding: 15px;
    }
    
    .item-title {
      font-size: 16px;
    }
  }
  
  @media (max-width: 480px) {
    .nav-left {
      gap: 15px;
    }
    
    .back-btn {
      padding: 6px 12px;
      font-size: 13px;
    }
    
    .page-title {
      font-size: 16px;
    }
    
    .list-wrapper {
      padding: 15px;
    }
    
    .item-header {
      flex-direction: column;
      align-items: flex-start;
      gap: 8px;
    }
    
    .item-title {
      margin-right: 0;
    }
  }
</style> 